package com.xc.hotelmgr.webapi.modules.dt.service.impl;

import com.google.common.collect.Lists;
import com.xc.hotelmgr.webapi.common.utils.R;
import com.xc.hotelmgr.webapi.modules.dt.entity.HtlDto;
import com.xc.hotelmgr.webapi.modules.ht.entity.HtHotelinfoEntity;
import com.xc.hotelmgr.webapi.modules.ht.service.HtHotelinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.Query;

import com.xc.hotelmgr.webapi.modules.dt.dao.HtlDao;
import com.xc.hotelmgr.webapi.modules.dt.entity.HtlEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.HtlService;


@Service("htlService")
public class HtlServiceImpl extends ServiceImpl<HtlDao, HtlEntity> implements HtlService {
    @Autowired
    private HtHotelinfoService htHotelinfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HtlEntity> page = this.page(
                new Query<HtlEntity>().getPage(params),
                new QueryWrapper<HtlEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils sqlQueryPage(Map<String, Object> params) {
        IPage<HtlEntity> page = new Query<HtlEntity>().getPage(params);
        params.put("page",page);
        List<HtlEntity> list = baseMapper.sqlQueyPage(params);
        return new  PageUtils(page,list);
    }

    @Override
    public HtlEntity sqlGetById(Integer id) {
        return baseMapper.sqlGetById(id);
    }

    @Override
    public R prepareSaveHtl(HtlDto htlDto) {
        List<HtlEntity> htlEntities = null;
        Map<String,Object> params = new HashMap<>();
        params.put("dtId",htlDto.getDtId());
        params.put("province",htlDto.getProvince());
        params.put("city",htlDto.getCity());
        params.put("county",htlDto.getCounty());
        params.put("brand",htlDto.getBrand());
        R r = null;
        //批量授权所有酒店
        if(htlDto.getAuthType().equals("1") && htlDto.getAuthAll().equals("1")) {
            htlEntities = Lists.newArrayList();
            HtlEntity htl = null;
            Long id = null;

            List<HtHotelinfoEntity> hotels = baseMapper.sqlQuryHotelInfo(params);
            if(hotels.size() <= 0){
                r = R.ok("所有酒店已经授权,本次操作无需要授权的酒店");
            }else {
                r = R.ok("本次授权了【"+ hotels.size() + "】个酒店,授权过程可能需要一段时间，请耐心等待！");
            }
            for (HtHotelinfoEntity h : hotels){
                htl = new HtlEntity();
                htl.setDtId(Integer.valueOf(htlDto.getDtId()));
                htl.setHotelCode(h.getId());
                id = Long.valueOf(h.getId().toString() + htlDto.getDtId());
                //id = Long.valueOf(htlDto.getDtId() + h.getId().toString());
                htl.setId(id);
                htlEntities.add(htl);
            }
        }
        //批量授权满足指定条件酒店
        if(htlDto.getAuthType().equals("1") && htlDto.getAuthAll().equals("0")){
            htlEntities = Lists.newArrayList();
            HtlEntity htl = null;
            Long id = null;
            List<HtHotelinfoEntity> hotels = baseMapper.sqlQuryHotelInfo(params);
            if(hotels.size() <= 0){
                r = R.ok("所有满足条件的酒店已经授权,本次操作无需要授权的酒店");
            }else {
                r = R.ok("本次授权了【"+ hotels.size() + "】个满足条件的酒店,授权过程可能需要一段时间，请耐心等待！");
            }
            for (HtHotelinfoEntity h : hotels){
                htl = new HtlEntity();
                htl.setDtId(Integer.valueOf(htlDto.getDtId()));
                htl.setHotelCode(h.getId());
                //id = Long.valueOf(htlDto.getDtId() + h.getId().toString());
                id = Long.valueOf(h.getId().toString() + htlDto.getDtId());
                htl.setId(id);
                htlEntities.add(htl);
            }
        }
        //授权指定酒店
        if(htlDto.getAuthType().equals("2")){
            htlEntities = Lists.newArrayList();
            HtlEntity htl = null;
            Long id = null;
            if(htlDto.getHotelIds().length <= 0){
                r = R.ok("请选择需要授权的酒店");
            }else {
                r = R.ok("本次授权了【"+ htlDto.getHotelIds().length + "】个酒店,授权过程可能需要一段时间，请耐心等待！");
            }
            for (String hid : htlDto.getHotelIds()){
                htl = new HtlEntity();
                htl.setDtId(Integer.valueOf(htlDto.getDtId()));
                htl.setHotelCode(Long.valueOf(hid));
                //id = Long.valueOf(htlDto.getDtId() + hid);
                id = Long.valueOf(hid + htlDto.getDtId());
                htl.setId(id);
                htlEntities.add(htl);
            }
        }
        r.put("htls", htlEntities);
        return r;
    }
    @Async
    @Override
    public void syncSaveHtl(List<HtlEntity> htlEntities){
        saveOrUpdateBatch(htlEntities);
    }

    @Override
    public List<HtlEntity> sqlQuery(String dtId, String district, String brand) {
        return baseMapper.sqlQuery(dtId,district,brand);
    }

    @Override
    public PageUtils sqlQuryHotelPage(Map<String, Object> params) {
        IPage<HtHotelinfoEntity> page = new Query<HtHotelinfoEntity>().getPage(params);
        String dtId = (String)params.get("dtId");
        String name = (String)params.get("name");
        String province = (String)params.get("province");
        String city = (String)params.get("city");
        String county = (String)params.get("county");
        List<HtHotelinfoEntity> list = baseMapper.sqlQuryhotelPage(page,dtId,name,province,city,county);

        return new PageUtils(page,list);
    }

    @Override
    public int sqlQueryStatus(Integer dtId,Long hotelCode) {
        return baseMapper.sqlQueryStatus(dtId,hotelCode);
    }
}